package com.gpy.hospital.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gpy.hospital.cms.dao.BingFangMapper;
import com.gpy.hospital.cms.pojo.BingFang;
import com.gpy.hospital.cms.pojo.User;
import com.gpy.hospital.cms.service.IXuanZuoService;
import com.gpy.hospital.cms.vo.XuanZuoVo;
import com.gpy.hospital.common.Const;
import com.gpy.hospital.common.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author
 * @Date 2020/3/27 10:27
 * @Desc
 **/
@Service
public class XuanZuoServiceImpl implements IXuanZuoService {

    @Autowired
    private BingFangMapper bingFangMapper;

    @Override
    public List<XuanZuoVo> getAll() {
        List<BingFang> bingFangList = bingFangMapper.getByLevel();
        List<XuanZuoVo> xuanZuoVoArrayList = new ArrayList<>();
        for (BingFang bingFang : bingFangList) {
            XuanZuoVo xuanZuoVo = new XuanZuoVo();
            xuanZuoVo.setBingFang(bingFang);

            List<BingFang> bingFang1 = bingFangMapper.getByTopLevel(bingFang.getId());

            xuanZuoVo.setBingFangList(bingFang1);
            xuanZuoVoArrayList.add(xuanZuoVo);
        }
        return xuanZuoVoArrayList;
    }

    @Override
    public ServerResponse confirmZuo(Integer id, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(Const.CURRENT_USER);
        int result = bingFangMapper.update(id, user.getEmail(), 2);
        if (result > 0) {
            return ServerResponse.createByErrorMessage("预定成功！");
        }
        return ServerResponse.createByErrorMessage("预定失败！");
    }

    @Override
    public PageInfo<BingFang> getAllBingFang(Integer pageNum, Integer pageSize, String num) {
        PageHelper.startPage(pageNum, pageSize);
        List<BingFang> bingFangList = bingFangMapper.getByBingFang(num);
        return new PageInfo<>(bingFangList);
    }

    @Override
    public BingFang getById(Integer id) {
        return bingFangMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateByNull(Integer id) {
        bingFangMapper.updateByNull(id);
    }

    @Override
    public ServerResponse delete(Integer id) {
        int result =  bingFangMapper.deleteByPrimaryKey(id);
        if (result > 0) {
            return ServerResponse.createByErrorMessage(" 成功！");
        }
        return ServerResponse.createByErrorMessage(" 失败！");
    }

    @Override
    public ServerResponse add(BingFang bingFang) {
        int result =  bingFangMapper.insert(bingFang);
        if (result > 0) {
            return ServerResponse.createByErrorMessage(" 成功！");
        }
        return ServerResponse.createByErrorMessage(" 失败！");
    }

    @Override
    public List<BingFang> getAllByLevel() {
        return bingFangMapper.getByLevel();
    }

    @Override
    public ServerResponse addss(BingFang bingFang) {
        bingFang.setStatus(1);
        bingFang.setLevel(2);
        BingFang bingFang1 = bingFangMapper.selectByPrimaryKey(bingFang.getTopId());
        bingFang.setLouCengNum(bingFang1.getId());
        int result =  bingFangMapper.insert(bingFang);
        if (result > 0) {
            return ServerResponse.createByErrorMessage(" 成功！");
        }
        return ServerResponse.createByErrorMessage(" 失败！");
    }
}
